<template>
  <div class="params">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体 -->
    <el-card class="mt16">
      <!-- 警告提示 -->
      <el-alert
        title="注意：只允许为三级分类设置相关参数！"
        show-icon
        type="warning"
        effect="dark"
      >
      </el-alert>
      <!-- 选择分类 -->
      <el-row class="mt16">
        <el-col :span="3">
          <p>选择商品分类</p>
        </el-col>

        <el-col :span="18">
          <p>选择分类ID:{{ checkArr }}----当前分类ID{{ current_cate_id }}</p>
          <el-cascader
            v-model="checkArr"
            :options="GoodsCateData"
            :props="defaultProps"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>

      <!-- tabs栏 -->
      <el-tabs
        v-model="activeName"
        @tab-click="handleClick"
        type="border-card"
        class="mt16"
      >
        <el-tab-pane label="动态参数" name="many">
          <el-row>
            <el-col>
              <el-button type="primary" size="mini" :disabled="isOk"
                >添加参数</el-button
              >
            </el-col>
          </el-row>
          <!-- 展开动态参数表格 -->
          <el-table :data="manyList" class="mt16" border>
            <el-table-column label="#" type="expand">
              <template slot-scope="scope">
                <div>
                  <el-tag
                    :key="index"
                    v-for="(item, index) in scope.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(tag)"
                  >
                    {{ item }}
                  </el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="inputVisible"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  >
                  </el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput"
                    >+ New Tag</el-button
                  >
                </div>
              </template>
            </el-table-column>
            <el-table-column label="#" type="index"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template>
                <div>
                  <el-button type="primary">编辑</el-button>
                  <el-button type="danger">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-row>
            <el-col>
              <el-button type="primary" size="mini">添加属性</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "params",
  data() {
    return {
      inputVisible: "",
      inputValue: "",
      //展示动态数据
      manyList: [],
      //展示静态数据
      onlyList: [],
      activeName: "many",

      checkArr: [],
      //数据源
      GoodsCateData: [],
      //配置项
      defaultProps: {
        expandTrigger: "click",
        label: "cat_name",
        value: "cat_id",
        children: "children",
      },
    };
  },
  computed: {
    isOk() {
      return this.checkArr.length !== 3;
    },
    current_cate_id() {
      return this.checkArr[this.checkArr.length - 1];
    },
  },
  methods: {
    //点击切换成输入框
    showInput() {
      console.log("点击切换成输入框");
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    //敲回车或者失焦时添加参数
    handleInputConfirm(row) {
      console.log("添加内容");
      const inputValue = this.inputValue;
      if (inputValue.length > 0) {
        row.attr_vals.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    //点击tag删除
    handleClose() {
      console.log("点击tag删除");
    },
    async getGoodsCateList() {
      const res = await this.$http.getGoodsCateListApi();
      console.log(res, "分类");
      this.GoodsCateData = res;
    },
    handleClick() {
      console.log(this.activeName);
      if (!this.isOk) {
        this.getGoodsParamsList();
      }
    },
    //监听级联
    handleChange() {
      console.log("监听级联");

      if (this.checkArr.length !== 3) {
        this.$message({
          message: "请输入三级菜单",
          type: "error",
        });
        return false;
      }
      //如果选择的分类级别是3级，则展示对应分离欸参数列表
      this.getGoodsParamsList();
    },
    //获取分类参数列表的封装
    async getGoodsParamsList() {
      if (this.activeName === "many") {
        //展示动态数据
        const res = await this.$http.getGoodsParamsListApi(
          this.current_cate_id
        );
        console.log(res, "展示动态数据");
        res.forEach((item) => {
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(",") : [];
        });
        this.manyList = res;
      } else if (this.activeName === "only") {
        //展示静态数据
      }
    },
  },
  mounted() {},
  created() {
    this.getGoodsCateList();
  },
};
</script>
<style scoped lang="scss">
.el-tag {
  margin: 10px;
}
.input-new-tag {
  width: 80px;
}
</style>
